package TuiLayer;
import java.util.Scanner;
import ControlLayer.*;
/**TuiLayer package sets the location for DVDUI class.
 * 
 * Scanner package is used here, because it provides tracking on the inputs from a
 * keyboard. This gives user(Private Person) the ability to have control on
 * DVDUI. The control is exercised by inserting different values through the
 * keyboard. This means that DVDUI behaves strictly in accordance to the user
 * preferences.
 * 
 * The ControlLayer package is imported, because TUIs must be able to access classes
 * in the ControlLayer - AddressController, RegisterController and LoanController.
 * ---------------------------------------------------------------------------------
 * DVDUI class is devided in 6 sections. In each section(except the first
 * one) methods with similar functionalities are placed.
 * 
 * Section Contents:
 *  Variables
 *  Constructors
 *      - start()
 *  UserTrackers
 *      - dvdMenu()
 *      - writedvdMenu()
 *  Methods
 *      - getDVD()
 *      - addDVD()
 *      - deleteDVD()
 *      - update()
 *      - listAllDvds()
 *  Inputs
 *      - inputContinue()
 *      - inputDVDID()
 *      - inputTitle()
 *      - inputArtist()
 *      - inputPublicationDate()
 *  Printers
 *      - writeError()
 */

public class DVDUI
{
  /** VARIABLES */
    //DVDUI has to send the user preferences as commands to RegisterController.
    //For the purpose to provide access to the controller the following object
    //reference(association) is needed.
    private RegisterController tellCtr = new RegisterController();
    //Reference to RegisterController.
    
  /** CONSTRUCTORS */
  public void start()
  //Launches the UI and initializes the optionChoice functionality. User is now
  //able to use the DVDUI.
  {
      dvdMenu();    //See User Tracker section.
  }
  
  /** USER TRACKERS */
  public void dvdMenu()
  //Provides the optionChoice functionality.
  {
      boolean exit = false;
      //This indicates whether the DVDUI is launched and running.
      
      while(!exit)
      //This can be interpreted like this: 'While the application is running perform
      //the following tasks below'. If the boolean expression exit becomes true,
      //then the interface stops working and the application is closed. User
      //cannot use the DVDUI anymore and goes back to MainMenu interface.
      {
          int optionChoice = writedvdMenu();
          //Before determing what the UI have to execute, it will acquire user
          //optionChoice first, by calling this method.
          
          if(optionChoice == 1)
          //User inputs 1 as an optionChoice - UI prompts user for input values
          //for attributes to determine searching details.
          {
              getDVD();
          }
          
          else if(optionChoice == 2)
          //User inputs 2 as an optionChoice - UI prompts user for DVD details.
          {
              addDVD();
          }
          
          else if(optionChoice == 3)
          //User inputs 3 as an optionChoice - UI deletes a DVD chosen by user.
          {
              deleteDVD();
          }
          
          else if(optionChoice == 4)
          //User inputs 4 as an optionChoice - UI update a DVD chosen by user.
          {
              update();
          }
          
          else if(optionChoice == 5)
          //User inputs 5 as an optionChoice - UI displays a list of all DVDs.
          {
              listAllDvds();
          }
          
          else if(optionChoice == 0)
          //User inputs 0 as an optionChoice - User is redirected back to the
          //MainMenuUI. 
          {
              exit = true; //Variable exit becomes true. UI shuts down.
          }
          else if(optionChoice != 1 && optionChoice != 2 && optionChoice != 3
                  && optionChoice != 4 && optionChoice != 5 && optionChoice !=0)
          //This checks whether the optionChoice number is a valid command. User 
          //makes invalid input(the number is not within the range from 0 to 5).
          //A message is displayed to notify about the problem.                        
          {
              writeError();
              inputContinue();
          }
      }
  }

  public int writedvdMenu()
  //Acquires optionChoice. It creates a keyboard object(of Scanner class), so 
  //that user can input her optionChoice. It is important to note that this
  //method also visualizes the UI.
  {
      Scanner keyboard = new Scanner(System.in);
      //Creates the keyboard object.
      
      System.out.println("\f ***DVD Menu***");
      System.out.println(" (1) Show information about the DVD");
      System.out.println(" (2) Add DVD");
      System.out.println(" (3) Delete DVD");
      System.out.println(" (4) Update DVD");
      System.out.println(" (5) List all DVD's");
      System.out.println(" (0) Return");
      System.out.print("\n Choose an option: ");
      //DVDUI visualization. It is made by printing these lines in the
      //Terminal window.
      
      String checkInput = keyboard.nextLine();
      //This tracks user inputs by calling Scanner nextLine() method on each input.
      
      int optionChoice; //Local variable, where user option choice will be stored.
      
      try
      //After getting user input, this try-catch structure checks whether the
      //input is a number(integer). This allows the use of integers, instead of 
      //strings for determing the user option choice and at the same time this
      //prevents system from crashing.
      {
          optionChoice = Integer.parseInt(checkInput);
          //If it is a number then UI gets an optionChoice to check whether it
          //there is such an option.
      }
      
      catch(NumberFormatException wrongInput)
      {
          optionChoice = -1;
          //If the input is not a number, then this code will make UI display a
          //message, to notify user about the problem.
      }
      
      return optionChoice; //Sends what is the user optionChoice.
  }
    
  /** METHODS */
  private void getDVD()
  //Defines searching details in accordance to user preferences.
  {
      String dvdID = inputDVDID();
      //Variable id is the definition for user search criteria.
      
      try
      //This try-catch structure ensures that the system will not crash. So, if
      //there is a DVD with the requested(by user) id it will be displayed.
      //Otherwise it will display a message that nothing is found.
      {
          System.out.println(tellCtr.printDVD(dvdID));
          //Tells the Controller to show user the search results.
          
          inputContinue();
        }
        
        catch(NullPointerException exp)
        {
            System.out.println("There is no DVD with ID: " + dvdID);
            //Displays the reason why there is no search result.
            
            inputContinue();
        }
    }
    
  private void addDVD()
  //Prompts user to add attributes for the DVD which is going to be added(created)
  //in the Register.
  {
      String dvd_id = "";
      String title = "";
      String artist = "";
      String publicationDate = "";
      
      dvd_id = inputDVDID();
      title = inputTitle();
      artist = inputArtist();
      publicationDate = inputPublicationDate();
      //For each variable, UI prompts first for a value, then user has to
      //input it. After all attributes are done, person is added in the
      //Register.
    
      tellCtr.createDVD(dvd_id, title, artist, publicationDate);
      //Tells the Controller to add the DVD to the Register with user defined 
      //attributes.
        
      inputContinue();
    }

  public void deleteDVD()
  //Prompts user to add attribute for id. If the id is valid then a DVD with such
  //an id will be removed from the Register.
  {
      String delete = inputDVDID();
      //Defines which DVD will be removed.
      
      try
      //This try-catch structure ensures that the system will not crash. So, if
      //there is a DVD with the requested(by user) id it will be removed.
      //Otherwise it will display a message that there is no DVD with such id.
      {
          tellCtr.deleteDVD(delete);
          //Tells Controller to remove the DVD.
          System.out.println("DVD has been deleted from database");
          //Notifies user for the successful execution of the operation.
        }
        catch(Exception notFound)
        {
            System.out.println("No such DVD exists");
            //Displays an error message.
        }
        
        inputContinue();
    }
  
  public void update()
  //Prompts user to add attribute for id. If the id is valid then user will be
  //llowed to change attributes of a DVD with such an id.
  {
      String dvdID = inputDVDID();
      //Defines which DVD's details will be updated.
      
      try
      //This try-catch structure ensures that the system will not crash. So, if
      //there is a DVD with the requested, user will be allowed to add the new
      //attributes for the DVD. Otherwise it will display a message that there is
      //no DVD with such id.
      {
          tellCtr.updatemyDVD(dvdID, inputTitle(), inputArtist(), 
                                inputPublicationDate());
          //Tells controller to change person attributes.
          System.out.println("The information about this DVD has been updated");
          //Notifies user for the successful execution of the operation.
        }
        catch(Exception e)
        {
            System.out.println("No such DVD exists!");
            //Displays an error message.
        }
        
      inputContinue();
    }
    
  private void listAllDvds()
  //Displays a list of all DVD in the Register.
  {
      tellCtr.listAllDvds();
      inputContinue();
  }
    
  /** INPUTS */
  public void inputContinue()
  //Prompts user to press Enter to continue with using the UI.
  {
      Scanner keyboard = new Scanner(System.in);
      System.out.println(" Press Enter to continue:  ");
      String code = keyboard.nextLine();
    }
    
  public String inputDVDID()
  //Prompts user to define a value for DVDID.
  {
      boolean ok = false;
      String dvd_id = "";
      while(!ok)
      {
          Scanner keyboard = new Scanner(System.in);
          System.out.println(" Write DVD ID:  ");
          try 
          {
              dvd_id = keyboard.nextLine();
              ok = true;
            }
            catch (Exception e)
            {
                System.out.println("It should be a number, please try again");
                String input = keyboard.nextLine();
            }
        }
        return dvd_id;
    }

  public String inputTitle()
  //Prompts user to define a value for title.
  {
      Scanner keyboard = new Scanner(System.in);
      System.out.println(" Write Title:  ");
      String title = keyboard.nextLine();
      return title;
    }
    
  public String inputArtist()
  //Prompts user to define a value for Artist.
  {
      Scanner keyboard = new Scanner(System.in);
      System.out.println(" Enter Artist name:  ");
      String artist = keyboard.nextLine();
      return artist;
    }

  public String inputPublicationDate()
  //Prompts user to define a value for publicationDate.
  {
      Scanner keyboard = new Scanner(System.in);
      System.out.println(" Enter Publication Date:  ");
      String publicationDate = keyboard.nextLine();
      return publicationDate;
  }
  
  /** PRINTERS */
  private void writeError()
  //Prints an error message in the terminal window.
  {
      System.out.println("\n [ERROR]:\n You can only insert\n numbers 1, 2, 3 , 4"
                                + " 5\n and 0 for an option choice."
                                + "\n\n");
      inputContinue();
  }
}